package com.example.wanandroid.room

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.example.wanandroid.entity.banner.Banner

@Database(entities = [Banner::class], version = 1, exportSchema = false)
abstract class AppRoomDatabase : RoomDatabase() {
    abstract fun bannerDao(): BannerDao

    companion object{
        @Volatile
        private var mDataBase: AppRoomDatabase? = null

        fun setup(context: Context) {
            if (mDataBase == null) {
                synchronized(this) {
                    if (mDataBase == null) {
                        mDataBase =
                            Room.databaseBuilder(context, AppRoomDatabase::class.java, "my_database.db")
                                .addMigrations(object : Migration(0, 1) {
                                    override fun migrate(db: SupportSQLiteDatabase) {
                                        //db.execSQL("DROP TABLE user")
                                    }
                                })
                                .allowMainThreadQueries()
                                .build()
                    }
                }
            }
        }

        fun getInstance() = mDataBase!!
    }
}
